package com.eDriveV2;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Statview extends Activity {
	
	private ArrayList<Integer> timeData = new ArrayList<Integer>();
	private ArrayList<Integer> speedData = new ArrayList<Integer>();
	private ArrayList<Double> effData = new ArrayList<Double>();
	private String efficiencyLabel ="";
	private String fileName;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.statview);
		Log.d("Statview","Start");
		Bundle extras = getIntent().getExtras();
	    fileName = extras.getString("fileName");
	    
	    XMLReader xml = new XMLReader(fileName);
	    ArrayList[] array = xml.readXML();
	    timeData = array[0];
	    speedData = array[2];
	    effData = array[3];
	    statAvgSpeed(speedData);
	    statAvgEff(effData);
	    statAvgTime(timeData);

	}
	private void statAvgSpeed(ArrayList<Integer> speedData) {
		   Log.d("Statview","Start AvgSpeed");
		double total_value = 0;
		double value = 0;
		for (int i = 0; i < speedData.size(); i++) {
			if(i==0){
			}
			total_value += speedData.get(i);
			Log.d("speedData", String.valueOf(speedData.get(i)));
		}
		value = (int)total_value / speedData.size();
		TextView MyStatSpeed = (TextView) findViewById(R.id.avg_speedTextView);
		MyStatSpeed.setText((int)value  + " km/h");	
	}
	private void statAvgTime(ArrayList<Integer> timeData2) {
		   Log.d("Statview","Start AvgTime");
		int firstTime=0;
		int lastTime=0;
		int timeLasted=0;
		firstTime=timeData2.get(0);
		lastTime=timeData2.get(timeData2.size()-1);
		timeLasted = lastTime-firstTime;
		timeLasted = timeLasted/60000;
		Log.d("timeData", String.valueOf(timeLasted));
		TextView MyStatTime = (TextView) findViewById(R.id.drive_time_label);
		MyStatTime.setText(String.valueOf(timeLasted));
	}	
	private void statAvgEff(ArrayList<Double> effData) {
		Log.d("Statview","Start AvgEff");
		String label = "";
		double total_value = 0.0;
		double value = 0.0;
		for (int i = 0; i < effData.size(); i++) {
			total_value = effData.get(i)+total_value;
			Log.d("effData", String.valueOf(effData.get(i)));
		}
		value = total_value/effData.size();
		if (value == 0.0) {
			label = "?";
		}
		if (value <= 5.0 && value > 0.0) {
			label = "A";
		} else if (value > 5.0 && value <= 7.0) {
			label = "B";
		} else if (value > 7.0 && value <= 10.0) {
			label = "C";
		} else if (value > 10.0 && value <= 14.0) {
			label = "D";
		} else if (value > 14.0 && value <= 20.0) {
			label = "E";
		} else if (value > 20.0 && value <= 30.0) {
			label = "F";
		} else if (value > 30.0) {
			label = "G";
		} else {
			label = "Error";
		}
		efficiencyLabel = label;
		ImageView MyStatEco = (ImageView) findViewById(R.id.avg_ecoImageView);
		if (label.equals("?")) {
			MyStatEco.setImageResource(R.drawable.label);
		} else if (label.equals("A")) {
			MyStatEco.setImageResource(R.drawable.label_a);
		} else if (label.equals("B")) {
			MyStatEco.setImageResource(R.drawable.label_b);
		} else if (label.equals("C")) {
			MyStatEco.setImageResource(R.drawable.label_c);
		} else if (label.equals("D")) {
			MyStatEco.setImageResource(R.drawable.label_d);
		} else if (label.equals("E")) {
			MyStatEco.setImageResource(R.drawable.label_e);
		} else if (label.equals("F")) {
			MyStatEco.setImageResource(R.drawable.label_f);
		} else if (label.equals("G")) {
			MyStatEco.setImageResource(R.drawable.label_g);
		} else if (label.equals("Error")) {
			MyStatEco.setImageResource(R.drawable.label);
		}
	}
	public void shareButtonStatView(View v) {
		String values = efficiencyLabel;
		String values2 = "";
		if (values.equals("A")) {
			values2 = "Look how I care for the environment! \n"
					+ " I just drove label: " + values;
		} else if (values.equals("B")) {
			values2 = "Look how I care for the environment.\n"
					+ "I just drove label: " + values + "\n"
					+ "It is not perfect, but I'm getting there";
		} else if (values.equals("C")) {
			values2 = "Look how I care for the environment.\n"
					+ " I just drove label: " + values + "\n"
					+ "But there is still a lot to improve";
		} else if (values.equals("D")) {
			values2 = "I need to care more for the environment, but I'm getting there.\n"
					+ " i just drove label: " + values;
		} else if (values.equals("E")) {
			values2 = "I need to care more for the environment, but I'm slowly getting there.\n"
					+ " i just drove label: " + values;
		} else if (values.equals("F")) {
			values2 = "I need to care more for the environment.\n"
					+ " I just drove label: " + values;
		} else if (values.equals("G")) {
			values2 = "I need to care more for the environment.\n"
					+ " I just drove label: " + values;
		} else{
			values2 = "Error. eDrive could not retrieve the efficiency of this ride.\n";
		}
		Log.d("Test:", "Begin shareButton()");
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		emailIntent.setType("text/plain");
		String Subject = "Edrive";
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (Subject));
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, values2);
		startActivity(emailIntent);
	}	
}